package com.wskeee.ec.components.skin
{
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.display.Sprite;
	
	import com.wskeee.ec.core.EComponent;
	import com.wskeee.ec.core.EGlobal;
	import com.wskeee.ec.interfaces.ISkinDrawer;
	
	public class BaseSkinDrawer implements ISkinDrawer
	{
		protected var target:EComponent;
		protected var ui:*;
		
		public function init(target:EComponent,ui:*):void
		{
			this.target = target;
			this.ui = ui;
		}
		
		protected function get drawer():Graphics
		{
			return Sprite(ui).graphics as Graphics;
		}
		
		//--------------------------------------------------------------------------
		//
		//  Method
		//
		//--------------------------------------------------------------------------
		//-----------------------------------------------------------------
		//  redraw
		//-----------------------------------------------------------------
		/**
		 * 在这里初始一些不经常重绘的操作，
		 * 当样式改时系统会调用这里重绘组件 
		 * 
		 */
		public function redraw():void 
		{
			//
		}
		
		//-----------------------------------------------------------------
		//  validate
		//-----------------------------------------------------------------
		/**
		 * 生效属性 
		 */
		public function validateProperties():void
		{
			//override
		}
		/**
		 * 生效大小 
		 */
		public function validateSize():void
		{
			//override
		}
		/**
		 * 生效显示 
		 */
		public function validateDisplayList():void
		{
			//override
		}
		
		//-----------------------------------------------------------------
		//  style
		//-----------------------------------------------------------------
		/**
		 * 从 target 组件获取 样式 
		 * @param name
		 * @return 
		 * 
		 */
		public function getStyle(name:String):Object
		{
			return target.getStyle(name);
		}
		
		/**
		 * 获取 skin  
		 * @param name 样式名
		 * @param instance 是否实例化
		 * @return 
		 * 
		 */
		public function getSkin(name:String,instance:Boolean=true):Object
		{
			var skin:Object;
			if(EGlobal.isUseIDESkin)
			{
				skin = getStyle(name);
				if(skin && skin is String)
					skin = EGlobal.resourceManager.getElementClass(String(skin),"ecskin");
				if(skin && skin is Class && instance) return new skin();
				if(skin && skin is DisplayObject)return skin;
			}
			
			return skin;
		}
		//-----------------------------------------------------------------
		//  destory
		//-----------------------------------------------------------------
		/**
		 * 销毁 drawer 
		 * 
		 */
		public function destory():void
		{
			//oerride
			drawer.clear();
			target = null;
			ui = null;
		}
	}
}